﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

namespace FYPDemo
{
    public partial class ForgotPassword : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                bindSecurityQuestionDDL();
            }
        }

        protected void btnSend_Click(object sender, EventArgs e)
        {
            string name = txtMemberName.Text;
            string email = txtEmail.Text;
            int securityQ = Convert.ToInt32(ddlSecurityQuestion.SelectedValue);
            string answer = txtAnswer.Text;
            DataTable result = new DataTable();
            string charRnd = null;
            string numRnd = null;
            string resetPassword = null;
            int result2 = 0;

            result = SecurityManagement.validateResetPassword(name, email, securityQ, answer);

            if (result != null)
            {
                string name2 = result.Rows[0][0].ToString();
                string email2 = result.Rows[0][1].ToString();
                charRnd = SecurityManagement.RandomString1();
                numRnd = SecurityManagement.RandomString2();
                resetPassword = charRnd + numRnd;
                result2 = SecurityManagement.ResetPassword(name, email, resetPassword);

                if (result2 > 0)
                {
                    string subject = "Your Password successfully Reset";
                    string body = "Dear Mr/Mrs " + name2 + ", <br /><br />  Kindly inform you that your password has been reset: ";
                    body = body + "Please log in using the information below! <br/><br/>";
                    body = body + "<b><span style=\"color:slateblue\">User Name: </span></b>" + name2 + "<br />" + "<br /> <br />";
                    body = body + "<b><span style=\"color:slateblue\">New Password: </span></b>" + resetPassword + "<br />" + "<br /> <br />";
                    body = body + "Thank you." + "<br />" + "<br />" + "<span style=\"font-style:italic; color:gray; font-size:11pt\"></span>";

                    SendMail.SendEmail(email2, subject, body);
                }
            }
        }

        protected void bindSecurityQuestionDDL()
        {
            DataTable dt = new DataTable();

            dt = SecurityManagement.listSecurityQuestion();

            ddlSecurityQuestion.DataSource = dt;
            ddlSecurityQuestion.DataTextField = "SecurityQuestion";
            ddlSecurityQuestion.DataValueField = "ID";
            ddlSecurityQuestion.DataBind();  
        }
    }
}